<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
	public Map getMap(List<Map> result , Map<String,String> deptMap){
		int sumCount = 0;
		//处理结合数据
		for(Map map : result){
		
			String count = map.get("count") == null ? "0" : map.get("count").toString();
		
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("name",deptMap.get(ksdm));
			map.put("cont",map.get("count"));
			sumCount = sumCount + Integer.parseInt(count);
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		resMap.put("sumCount",sumCount+"");
		return resMap;
	}
%>
<%
	//统计住院人数
	String yydm =  input.getDesc("yydm","02");//医院代码
	String date = input.getDesc("start_date","2018-04-07");//开始时间
	String endDa = input.getDesc("end_date","2018-04-07");//结束时间
	String ksdm =  input.getDesc("ksdm","-1");//科室代码
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	
	String sqlWhere = "";
	if(!yydm.equals("-1"))
	sqlWhere += " and hz.ksdm in (select  id from YY_KSBMK where yydm ='"+yydm+"') " ;
	if(!ksdm.equals("-1")){
		sqlWhere += " and hz.ksdm in(" + ksdm + ")";
		
	}
	String deptSql = "select new map(id as id , name as name ) from his_dept where yydm ='"+yydm+"' ";
	if(yydm.equals("-1"))
		deptSql = "select new map(id as id , name as name ) from his_dept ";
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql(deptSql);
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	
	Map<String,Object> resMap = new HashMap<String,Object>();
	String sql ="SELECT DISTINCT\n" +
        "\thz.ksdm as ksdm,\n" +
        "\tSUM (ryrs) as 今日入院人数,\n" +
        "\tSUM (cyrs) as 今日出院人数,\n" +
        "\tSUM (zyrs) as 当前在院人数\n" +
        "FROM\n" +
        "\tBI_YZCX_ZYPT_HZ hz,\n" +
        "\tBI_YZCX_ZYPT_MX mx,\n" +
        "\tYY_KSBMK b (nolock)\n" +
        "WHERE\n" +
        "\thz.bqdm COLLATE Chinese_PRC_BIN = mx.bqdm\n" +
        "AND hz.ksdm COLLATE Chinese_PRC_BIN = mx.ksdm\n" +
        "AND hz.tjrq = mx.tjrq\n" +
        "AND hz.tjrq  >='"+nowDate+"'\n and hz.tjrq<='"+nowEndDa+"' " +
        "AND hz.ksdm COLLATE Chinese_PRC_BIN = b.id " +sqlWhere+" \n"+
        "GROUP BY\n" +
        "\thz.ksdm,\n" +
        "\tb.name\n" +
        "ORDER BY\n" +
        "\tSUM (ryrs) DESC";
	
	List<Map> resultList = null;
	System.out.println(sql);
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
      
	try {
		 ss.beginTransaction();
	
		 SQLQuery lbsqlQuery = ss.createSQLQuery(sql);
		 lbsqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         resultList = lbsqlQuery.list();
		 
		 ss.getTransaction().commit();
		 ss.close();
		
    }catch (Exception e){
	    ss.getTransaction().commit();
		 ss.close();
    }
	System.out.println(resultList);
	
	List<Map> result = new ArrayList<Map>();
	List<Map> result2 = new ArrayList<Map>();
	List<Map> result3 = new ArrayList<Map>();
	
	for(Map map : resultList){
		Map testmap = new HashMap<String,Object>();
		testmap.put("count",map.get("今日入院人数"));
		testmap.put("ksdm",map.get("ksdm"));
		result.add(testmap);
		
		
		Map testmap2 = new HashMap<String,Object>();
		testmap2.put("count",map.get("今日出院人数"));
		testmap2.put("ksdm",map.get("ksdm"));
		result2.add(testmap2);
		
		
		Map testmap3 = new HashMap<String,Object>();
		testmap3.put("count",map.get("当前在院人数"));
		testmap3.put("ksdm",map.get("ksdm"));
		result3.add(testmap3);
	}
	
	resMap.put("ryMap",getMap(result , deptMap));
	resMap.put("cyMap",getMap(result2 , deptMap));
	
	resMap.put("zyMap",getMap(result3 , deptMap));
	
	
	
	Message msg = new Message();
	
	msg.put("inpatient",resMap);
	out.println(msg);
%>